package com.example.vv.model.setupTree.base.presenter;

import com.example.vv.fragmentWork.entity.TreeEntity;
import com.example.vv.fragmentWork.mvp.MvpPresenter;
import com.example.vv.model.setupTree.base.contract.SetupTreeContract;
import com.example.vv.model.setupTree.base.model.SetupTreeModel;
import com.example.vv.res.net.BaseResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class SetupTreePresenter extends MvpPresenter<SetupTreeContract.View,SetupTreeContract.Model> implements SetupTreeContract.Presenter {

    @Override
    public void attach(SetupTreeContract.View view) {
        super.attach(view);
        model = new SetupTreeModel();
    }

    @Override
    public void setTreeTitle(String api) {
        model.GET_TREE_TITLE(api)
                .map(new Function<BaseResponse, List<TreeEntity>>() {
                    @Override
                    public List<TreeEntity> apply(BaseResponse baseResponse) throws Exception {
                        return new Gson().fromJson(baseResponse.getData(),new TypeToken<List<TreeEntity>>(){}.getType());
                    }
                })
                .subscribe(new Consumer<List<TreeEntity>>() {
                    @Override
                    public void accept(List<TreeEntity> treeEntityList) throws Exception {
                        view().getTreeTitle(treeEntityList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }

}
